﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using TSFileOutput.Core;

namespace TSFileOutput
{
    public static class TSConfigurator
    {
        #region Variables
        private static string errmsg = string.Empty;
        private static string archieved_path = string.Empty;
        private static string output_path = string.Empty;
        private static string logfile_path = string.Empty;
        private static string dbconn_str = string.Empty;
        private static TypeOfDatabase dbtype = TypeOfDatabase.MSSQL;
        private static bool email_active = false;
        private static string emailSMTP = string.Empty;
        private static string emailFromName = string.Empty;
        private static string emailFromAdd = string.Empty;
        private static string emailFromPass = string.Empty;
        private static string providername = string.Empty;

        private static string fromdate = "01/01/2013";
        private static string todate = DateTime.Now.ToString("MM/dd/yyyy");

        public static string ErrMsg
        {
            get { return errmsg; }
            set { errmsg = value; }
        }

        public static string PathForArchievedFiles
        {
            get { return archieved_path; }
        }

        public static string PathForOutputFiles
        {
            get { return output_path; }
        }

        public static string PathForLogFiles
        {
            get { return logfile_path; }
        }

        public static string DatabaseConnection
        {
            get { return dbconn_str; }
        }

        public static string ProviderName
        {
            get { return providername; }
        }

        public static TypeOfDatabase DatabaseType
        {
            get { return dbtype; }
        }

        public static string SMTP
        {
            get { return emailSMTP; }
        }

        public static bool Email_Active
        {
            get { return email_active; }
        }

        public static string EmailFromName
        {
            get { return emailFromName; }
        }

        public static string EmailFromAdd
        {
            get { return emailFromAdd; }
        }

        public static string EmailFromPass
        {
            get { return emailFromPass; }
        }

        public static string FromDate
        {
            get { return fromdate; }
            set { fromdate = value; }
        }

        public static string ToDate
        {
            get { return todate; }
            set { todate = value; }
        }

        #endregion

        public static bool ReadConfiguration()
        {
            bool smooth = true;

            try
            {
                XmlTextReader Reader = new XmlTextReader("sysconfig.xml");

                while (Reader.Read())
                {
                    if (Reader.NodeType == XmlNodeType.Element)
                    {
                        switch (Reader.Name)
                        {
                            case "Archieved":
                                archieved_path = Reader.ReadString();
                                break;

                            case "Out":
                                output_path = Reader.ReadString();
                                break;

                            case "Log":
                                logfile_path = Reader.ReadString();
                                break;

                            case "DBConn":
                                dbconn_str = Reader.ReadString();
                                break;

                            case "ProviderName":
                                providername = Reader.ReadString();
                                break;

                            case "EmailActive":
                                email_active = (Reader.ReadString().CompareTo("1") == 0);
                                break;

                            case "EmailSMTP":
                                emailSMTP = Reader.ReadString();
                                break;

                            case "EmailFromName":
                                emailFromName = Reader.ReadString();;
                                break;

                            case "EmailFromAdd":
                                emailFromAdd = Reader.ReadString();
                                break;

                            case "EmailFromPass":
                                emailFromPass = Reader.ReadString();
                                break;

                            case "DBType":

                                switch (Reader.ReadString())
                                {
                                    case "MSSQL":
                                        dbtype = TypeOfDatabase.MSSQL;
                                        break;

                                    case "ODBC":
                                        dbtype = TypeOfDatabase.ODBC;
                                        break;

                                    case "OleDB":
                                        dbtype = TypeOfDatabase.OleDB;
                                        break;
                                }

                                break;


                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrMsg = "[DaemonConfigurator.ReadConfiguration] : Exception : " + ex.Message;
            }

            return smooth;

        }

        public static bool FileCreate(string path, byte[] FileContent)
        {
            try
            {
                FileStream fs = File.Create(path);
                fs.Write(FileContent, 0, FileContent.Length);
                fs.Flush();
                fs.Close();
                fs = null;
            }
            catch (Exception ex)
            {
                //this.ShowErrorMessage("Create supporting document error: " + ex.Message);
                return false;
            }
            return true;
        }
    }
}
